home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / sbin / dhclient-script < prev    next >
Text File  |  2008-08-25  |  8KB  |  271 lines

  1. #!/bin/bash
  2.  
  3. # dhclient-script for Linux. Dan Halbert, March, 1997.
  4. # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
  5. # Modified for Debian.  Matt Zimmerman and Eloy Paris, December 2003
  6. # Modified to remove useless tests for antiquated kernel versions that
  7. # this doesn't even work with anyway, and introduces a dependency on /usr
  8. # being mounted, which causes cosmetic errors on hosts that NFS mount /usr
  9. # Andrew Pollock, February 2005
  10. # Modified to work on point-to-point links. Andrew Pollock, June 2005
  11. # Modified to support passing the parameters called with to the hooks. Andrew Pollock, November 2005
  12.  
  13. # The alias handling in here probably still sucks. -mdz
  14.  
  15. make_resolv_conf() {
  16.     if [ -n "$new_domain_name" -o -n "$new_domain_name_servers" ]; then
  17.         # Find out whether we are going to mount / rw
  18.         exec 9>&0 </etc/fstab
  19.         rootmode=rw
  20.         while read dev mnt type opts dump pass junk; do
  21.             [ "$mnt" != / ] && continue
  22.             case "$opts" in
  23.                 ro|ro,*|*,ro|*,ro,*)
  24.                    rootmode=ro
  25.                    ;;
  26.                  esac
  27.          done
  28.          exec 0>&9 9>&-
  29.  
  30.         # Wait for /etc/resolv.conf to become writable
  31.         if [ "$rootmode" = "rw" ]; then
  32.             while [ ! -w /etc ]; do
  33.                 sleep 0.1
  34.             done
  35.         fi
  36.  
  37.         local new_resolv_conf=/etc/resolv.conf.dhclient-new
  38.         rm -f $new_resolv_conf
  39.         if [ -n "$new_domain_name" ]; then
  40.             echo domain ${new_domain_name%% *} >>$new_resolv_conf
  41.         else # keep old domain
  42.             egrep -i '^ *[[:space:]]*domain' /etc/resolv.conf >> $new_resolv_conf
  43.         fi
  44.     if [ -n "$new_domain_search" ]; then
  45.         new_domain_search="${new_domain_search% }"
  46.         if [ -n "$new_domain_name" ]; then
  47.             new_domain_search="$new_domain_name $new_domain_search"
  48.         fi
  49.         echo "search ${new_domain_search//\\032/ }" >> $new_resolv_conf
  50.     elif [ -n "$new_domain_name" ]; then
  51.         echo "search ${new_domain_name}" >> $new_resolv_conf
  52.         else # keep old search list
  53.             egrep -i '^ *[[:space:]]*search' /etc/resolv.conf >> $new_resolv_conf
  54.     fi
  55.         if [ -n "$new_domain_name_servers" ]; then
  56.                    for nameserver in $new_domain_name_servers; do
  57.                        echo nameserver $nameserver >>$new_resolv_conf
  58.             done
  59.         else # keep 'old' nameservers
  60.             sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p /etc/resolv.conf >>$new_resolv_conf
  61.         fi
  62.         chown --reference=/etc/resolv.conf $new_resolv_conf
  63.         chmod --reference=/etc/resolv.conf $new_resolv_conf
  64.         mv -f $new_resolv_conf /etc/resolv.conf
  65.     fi
  66. }
  67.  
  68. run_hook() {
  69.     local script="$1"
  70.     local exit_status
  71.     shift    # discard the first argument, then the rest are the script's
  72.  
  73.     if [ -f $script ]; then
  74.         . $script "$@"
  75.     fi
  76.  
  77.  
  78.     if [ -n "$exit_status" ] && [ "$exit_status" -ne 0 ]; then
  79.         logger -p daemon.err "$script returned non-zero exit status $exit_status"
  80.         save_exit_status=$exit_status
  81.     fi
  82.  
  83.     return $exit_status
  84. }
  85.  
  86. run_hookdir() {
  87.     local dir="$1"
  88.     local exit_status
  89.     shift    # See run_hook
  90.  
  91.     if [ -d "$dir" ]; then
  92.         for script in $(run-parts --list $dir); do
  93.             run_hook $script "$@" || true
  94.             exit_status=$?
  95.         done
  96.     fi
  97.  
  98.     return $exit_status
  99. }
  100.  
  101. # Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
  102. exit_with_hooks() {
  103.     exit_status=$1
  104.  
  105.     # Source the documented exit-hook script, if it exists
  106.     if ! run_hook /etc/dhcp3/dhclient-exit-hooks "$@"; then
  107.         exit_status=$?
  108.     fi
  109.  
  110.     # Now run scripts in the Debian-specific directory.
  111.     if ! run_hookdir /etc/dhcp3/dhclient-exit-hooks.d "$@"; then
  112.         exit_status=$?
  113.     fi
  114.  
  115.     exit $exit_status
  116. }
  117.  
  118. set_hostname() {
  119.     local current_hostname=$(hostname)
  120.     if [ -z "$current_hostname" -o "$current_hostname" = "(none)" ]; then
  121.         hostname "$new_host_name"
  122.     fi
  123. }
  124.  
  125. if [ -n "$new_broadcast_address" ]; then
  126.     new_broadcast_arg="broadcast $new_broadcast_address"
  127. fi
  128. if [ -n "$old_broadcast_address" ]; then
  129.     old_broadcast_arg="broadcast $old_broadcast_address"
  130. fi
  131. if [ -n "$new_subnet_mask" ]; then
  132.     new_subnet_arg="netmask $new_subnet_mask"
  133. fi
  134. if [ -n "$old_subnet_mask" ]; then
  135.     old_subnet_arg="netmask $old_subnet_mask"
  136. fi
  137. if [ -n "$alias_subnet_mask" ]; then
  138.     alias_subnet_arg="netmask $alias_subnet_mask"
  139. fi
  140. if [ -n "$new_interface_mtu" ] && [ $new_interface_mtu -ge 575 ]; then
  141.     mtu_arg="mtu $new_interface_mtu"
  142. fi
  143. if [ -n "$IF_METRIC" ]; then
  144.     metric_arg="metric $IF_METRIC"    # interfaces(5), "metric" option
  145. fi
  146.  
  147.  
  148. # The action starts here
  149.  
  150. # Invoke the local dhcp client enter hooks, if they exist.
  151. run_hook /etc/dhcp3/dhclient-enter-hooks
  152. run_hookdir /etc/dhcp3/dhclient-enter-hooks.d
  153.  
  154. # Execute the operation
  155. case "$reason" in
  156.     MEDIUM|ARPCHECK|ARPSEND)
  157.         # Do nothing
  158.         ;;
  159.     PREINIT)
  160.         # The DHCP client is requesting that an interface be
  161.         # configured as required in order to send packets prior to
  162.         # receiving an actual address. - dhclient-script(8)
  163.  
  164.         if [ -n "$alias_ip_address" ]; then
  165.             # Bring down alias interface. Its routes will disappear too.
  166.             ifconfig $interface:0- inet 0
  167.         fi
  168.         ifconfig $interface inet 0 up
  169.  
  170.         # We need to give the kernel some time to get the interface up.
  171.         sleep 1
  172.         ;;
  173.     BOUND|RENEW|REBIND|REBOOT)
  174.  
  175.         set_hostname
  176.  
  177.         if [ -n "$old_ip_address" -a -n "$alias_ip_address" -a \
  178.              "$alias_ip_address" != "$old_ip_address" ]; then
  179.             # Possible new alias. Remove old alias.
  180.             ifconfig $interface:0- inet 0
  181.         fi
  182.  
  183.         if [ -n "$old_ip_address" -a \
  184.              "$old_ip_address" != "$new_ip_address" ]; then
  185.             # IP address changed. Bringing down the interface will delete all routes,
  186.             # and clear the ARP cache.
  187.             ifconfig $interface inet 0
  188.  
  189.         fi
  190.  
  191.         if [ -z "$old_ip_address" -o "$old_ip_address" != "$new_ip_address" -o \
  192.             "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then
  193.  
  194.             ifconfig $interface inet $new_ip_address $new_subnet_arg \
  195.                 $new_broadcast_arg $mtu_arg
  196.  
  197.         # point to point
  198.         if [ "$new_subnet_mask" == "255.255.255.255" ]; then
  199.             for router in $new_routers; do
  200.                 route add -host $router dev $interface
  201.             done
  202.         fi
  203.  
  204.             for router in $new_routers; do
  205.                 route add default dev $interface gw $router $metric_arg
  206.             done
  207.         fi
  208.  
  209.         if [ "$new_ip_address" != "$alias_ip_address" -a -n "$alias_ip_address" ];
  210.             then
  211.             ifconfig $interface:0- inet 0
  212.             ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  213.             route add -host $alias_ip_address $interface:0
  214.         fi
  215.  
  216.         make_resolv_conf
  217.  
  218.         ;;
  219.  
  220.     EXPIRE|FAIL|RELEASE|STOP)
  221.         if [ -n "$alias_ip_address" ]; then
  222.             # Turn off alias interface.
  223.             ifconfig $interface:0- inet 0
  224.         fi
  225.  
  226.         if [ -n "$old_ip_address" ]; then
  227.             # Shut down interface, which will delete routes and clear arp cache.
  228.             ifconfig $interface inet 0
  229.         fi
  230.  
  231.         if [ -n "$alias_ip_address" ]; then
  232.             ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  233.             route add -host $alias_ip_address $interface:0
  234.         fi
  235.  
  236.         ;;
  237.  
  238.     TIMEOUT)
  239.         if [ -n "$alias_ip_address" ]; then
  240.             ifconfig $interface:0- inet 0
  241.         fi
  242.  
  243.         ifconfig $interface inet $new_ip_address $new_subnet_arg \
  244.             $new_broadcast_arg $mtu_arg
  245.  
  246.         set -- $new_routers
  247.         first_router="$1"
  248.  
  249.         if [ -z "$first_router" ] || ping -q -c 1 $first_router; then
  250.             if [ "$new_ip_address" != "$alias_ip_address" -a \
  251.                 -n "$alias_ip_address" ]; then
  252.                 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  253.                 route add -host $alias_ip_address dev $interface:0
  254.             fi
  255.         
  256.             for router in $new_routers; do
  257.                 route add default dev $interface gw $router $metric_arg
  258.             done
  259.  
  260.             make_resolv_conf
  261.         else
  262.             # Changed from 'ifconfig $interface inet 0 down' - see Debian bug #144666
  263.             ifconfig $interface inet 0
  264.             exit_with_hooks 2 "$@"
  265.         fi
  266.  
  267.         ;;
  268. esac
  269.  
  270. exit_with_hooks 0
  271.